import { onMounted, onUnmounted, ref } from "vue";

export default function useClock() {
  const clock = ref(new Date());
  let clockID = null;
  const intervalCounter = () => {
    clockID = setInterval(() => {
      clock.value = new Date();
    }, 1000);
  };
  const clearIntervalCounter = () => {
    clearInterval(clockID);
  };

  // 在 setup 内注册生命周期钩子
  onMounted(intervalCounter);
  onUnmounted(clearIntervalCounter);

  return { clock };
}
